import {OperateType,genrateId} from '../util/common.js'
import {readJson,writeJSON} from '../util/PromiseJson.js'
import path from 'path'
import __dirname from '../util/dirname.js'
const baseDir = path.resolve(__dirname,'../data/maps')
const saveMap = async (req,res)=>{
  const {mapInfo,operate} = req.body
  if(operate === OperateType.Create){
    await createMap(mapInfo,res)
  }else if(operate === OperateType.Edit){
    await editMap(mapInfo,res)
  }
  // res.send({code:200,msg:'ok'})
}

async function createMap(mapInfo,res){
  const id = genrateId()
  const target = path.join(baseDir,`${id}.json`)
  mapInfo.id = id
  await writeJSON(target,JSON.stringify(mapInfo))
  res.send({code:200,msg:'ok',data:id})
}

async function editMap(mapInfo,res){

}



export {saveMap}